package org.example.aspec;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.example.ann.DynamicExecutor;
import org.example.ann.DynamicJob;
import org.example.config.ExecutorConfig;
import org.example.service.XxlJobConfigService;
import org.example.service.impl.E;
import org.example.util.Opt;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.lang.reflect.Method;

/**
 * @author zengxiaoyu
 * @date 2025/9/11 16:58
 */
@EnableAspectJAutoProxy
@Aspect
@Configuration
@RequiredArgsConstructor
@Slf4j
public class XxlJobAspect {

    private final XxlJobConfigService xxlJobConfigService;

    //@Pointcut("execution(* org.example.job.*.*(..))")

    // 使用 注解 XxlJobExecutor 的方法
    @Pointcut("@annotation(org.example.ann.XxlJobExecutor)")
    public void xxlJobExecutorPointcut(){

    }


//    @Pointcut("@annotation(org.example.ann.DynamicJob)")
//    public void dynamicJobPointcut(){
//
//    }
//    @Before("dynamicJobPointcut()")
//    public void  aroundXxlJobExecutorMethod(JoinPoint joinPoint){
//        // joinPoint获取  方法
//        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
//        Method method = signature.getMethod();
//        DynamicJob dynamicJob = method.getAnnotation(DynamicJob.class);
//        XxlJob xxlJob = method.getAnnotation(XxlJob.class);
//        DynamicExecutor executor = dynamicJob.executor();
//        if(xxlJob ==null){
//             log.info("DynamicJob方法上 没有找到 XxlJob 注解");
//             throw new RuntimeException("DynamicJob方法上 没有找到 XxlJob 注解");
//        }
//        if(executor ==null){
//            log.info("DynamicJob方法上 没有找到 DynamicExecutor 注解");
//            throw new RuntimeException("DynamicJob方法上 没有找到 DynamicExecutor 注解");
//        }
//        if(StringUtils.isEmpty(executor.name())){
//            log.info("DynamicExecutor 注解的 name 属性不能为空");
//        }
//        try {
//            xxlJobConfigService.createJobIfNotExist(dynamicJob,xxlJob);
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }

    // 获取方法返回值   处理
    @Around("xxlJobExecutorPointcut()")
    public Object aroundXxlJobExecutorMethod(ProceedingJoinPoint joinPoint){
        Object result = null;
        try {
            // 执行目标方法
            result = joinPoint.proceed();
            Opt.of(result).action(t->{
                if(t instanceof ExecutorConfig){
                    try {
                        xxlJobConfigService.createIfNotExist((ExecutorConfig) t);
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
                log.info("xxlJobExecutorPointcut result:{},type:{}",t,t.getClass().getCanonicalName());
            });
        } catch (Throwable exception) {
            // 处理异常情况
            exception.printStackTrace();
            log.error("xxlJobExecutorPointcut error:{}",exception.getMessage());
            throw new RuntimeException(exception);
        }
        return result;
    }



}
